﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MsPaint : MonoBehaviour
{
    private LineRenderer currentLine;//当前 线

    public Material lineMaterial;//线 材质
    public Color lineColor = Color.yellow;
    public float lineSize = 0.1f;

    List<Vector3> positions = new List<Vector3>();
    public bool isMouseDown = false;//判断是否按下（是否开始绘制）

    private Vector3 lastPositionPoint = Vector3.zero;//绘制前的点
    private float lineDistence = 0.02f;

    GameObject color;
    GameObject size;

    private void Start()
    {
        color = GameObject.Find("Canvas").transform.Find("Color").gameObject;
        size = GameObject.Find("Canvas").transform.Find("Size").gameObject;

        GameObject.Find("Canvas").transform.Find("ColorBtn").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            color.SetActive(true);
        });
        GameObject.Find("Canvas").transform.Find("SizeBtn").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            size.SetActive(true);
        });
        GameObject.Find("Canvas").transform.Find("Color/红").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            lineColor = Color.red;
            color.SetActive(false);
        });
        GameObject.Find("Canvas").transform.Find("Color/黄").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            lineColor = Color.yellow;
            color.SetActive(false);
        });
        GameObject.Find("Canvas").transform.Find("Color/蓝").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            lineColor = Color.blue;
            color.SetActive(false);
        });
        GameObject.Find("Canvas").transform.Find("Size/01").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            lineSize = 0.1f;
            size.SetActive(false);
        });
        GameObject.Find("Canvas").transform.Find("Size/02").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            lineSize = 0.2f;
            size.SetActive(false);
        });
        GameObject.Find("Canvas").transform.Find("Size/04").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            lineSize = 0.4f;
            size.SetActive(false);
        });
        GameObject.Find("Canvas").transform.Find("清除").gameObject.GetComponent<Button>().onClick.AddListener(delegate
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                Destroy(transform.GetChild(i).gameObject);
            }
        });
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SceneManager.LoadScene("TwoMenu");
        }
        //绘制起始点
        if (Input.GetMouseButtonDown(0))
        {
            //创建LineRenderer
            GameObject go = new GameObject();
            go.transform.SetParent(this.transform);
            currentLine = go.AddComponent<LineRenderer>();

            //设置LineRender基本属性
            currentLine.material = lineMaterial;
            currentLine.startColor = lineColor;
            currentLine.endColor = lineColor;
            currentLine.startWidth = lineSize;
            currentLine.endWidth = lineSize;
            currentLine.numCapVertices = 5;
            currentLine.numCornerVertices = 5;

            Vector3 position = GetMousePoint();
            AddPoint(position);
            isMouseDown = true;
            lineDistence += 0.02f;
        }

        //绘制中
        if (isMouseDown)
        {
            Vector3 position = GetMousePoint();
            //当前点与前一个的距离小于0.1f时添加点到集合
            if (Vector3.Distance(position, lastPositionPoint) > 0.1f)
                AddPoint(position);
        }

        //绘制结束点
        if (Input.GetMouseButtonUp(0))
        {
            currentLine = null;
            positions.Clear();
            isMouseDown = false;
        }

    }

    //添加顶点个数
    void AddPoint(Vector3 position)
    {
        position.z -= lineDistence;
        positions.Add(position);
        currentLine.positionCount = positions.Count;//设置定点数
        currentLine.SetPositions(positions.ToArray());
        lastPositionPoint = position;
    }
    //获取顶点坐标
    Vector3 GetMousePoint()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool isCollider = Physics.Raycast(ray, out hit, Mathf.Infinity);
        if (isCollider)
        {
            return hit.point;
        }
        Debug.Log("返回默认坐标点");
        return Vector3.zero;
    }

    //public void OnRedColor(bool IsOn)
    //{
    //    if (IsOn)
    //    {
    //        lineColor = Color.red;
    //    }
    //}
    //public void OnGreenColor(bool IsOn)
    //{
    //    if (IsOn)
    //    {
    //        lineColor = Color.green;
    //    }
    //}
    //public void OnBlueColor(bool IsOn)
    //{
    //    if (IsOn)
    //    {
    //        lineColor = Color.blue;
    //    }
    //}
    //public void OnWhiteColor(bool IsOn)
    //{
    //    if (IsOn)
    //    {
    //        lineColor = Color.white;
    //    }
    //}

    //public void On1Size(bool IsOn)
    //{
    //    if (IsOn)
    //    {
    //        lineSize = 0.1f;
    //    }
    //}
    //public void On2Size(bool IsOn)
    //{
    //    if (IsOn)
    //    {
    //        lineSize = 0.2f;
    //    }
    //}
    //public void On4Size(bool IsOn)
    //{
    //    if (IsOn)
    //    {
    //        lineSize = 0.4f;
    //    }
    //}
}
